package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import cc.edt.iceutils3.json.IceFastJsonUtils;
import cc.edt.iceutils3.net.IceOkHttpUtils;
import com.alibaba.fastjson.JSON;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.OrderChannel;
import com.edt.ichannel.api.service.GawoTwoService;
import com.edt.ichannel.api.service.InterfaceLogService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 肖瑶
 */
@Service
public class GawoTwoServiceImpl implements GawoTwoService {

    private Logger logger = LogManager.getLogger(GawoTwoServiceImpl.class);

    @Resource
    private InterfaceLogService logService;

    /**
     * 工单二期意向单收集
     * @param orderChannel 渠道下单实体
     * @return 返回值
     */
    @Override
    public ActionResult collectorder(OrderChannel orderChannel) {
        ActionResult actionResult = new ActionResult();
        if (actionResult == null) {
            return actionResult;
        }
        if(orderChannel == null){
            logger.info("collectOrder异常：下单实体为空"+orderChannel );
            actionResult.setMessage("collectOrder异常：下单实体为空");
            actionResult.setSuccess(false);
            return actionResult;
        }
        String json = null;
        //判断订单类型,是否走此接口
        if(IceStringUtils.isBlank(orderChannel.getBusinessId())){
            actionResult.setMessage("collectOrder异常：businessid为空");
            actionResult.setSuccess(false);
            return actionResult;
        }
        json =getCollectOrderJson(orderChannel);
        if(json == null){
            logger.info("orderChannel json:"+json);
            actionResult.setMessage("collectOrder异常：businessid类型错误");
            actionResult.setSuccess(false);
            return actionResult;
        }
        try {
            logger.info("orderChannel json:"+json);
            actionResult = JSON.parseObject(IceOkHttpUtils.doHttpPost(CommonConstant.URL_GAWOTWO_COLLECTORDER_PASS, json, 30000), ActionResult.class);
            logger.info("collectOrder返回结果：" + IceFastJsonUtils.toJsonString(actionResult));
        } catch (Exception e) {
            logger.info("collectOrder异常：" + e.getMessage());
            actionResult.setMessage("collectOrder异常：" + e.getMessage());
            actionResult.setSuccess(false);
        }
        logService.addLog(actionResult, json, CommonConstant.URL_GAWOTWO_COLLECTORDER_PASS, CommonConstant.INTERFACE_TYPE_GAWOTWOCOLLECT_PASS);
        return actionResult;
    }

    @Override
    public ActionResult formalOrder(OrderChannel orderChannel) {
        ActionResult actionResult = new ActionResult();
        if (actionResult == null) {
            return actionResult;
        }
        if(orderChannel == null){
            logger.info("collectOrder异常：下单实体为空"+orderChannel );
            actionResult.setMessage("collectOrder异常：下单实体为空");
            actionResult.setSuccess(false);
            return actionResult;
        }
        String json = null;
        //判断订单类型,是否走此接口
        if(IceStringUtils.isBlank(orderChannel.getBusinessId())){
            actionResult.setMessage("collectOrder异常：businessid为空");
            actionResult.setSuccess(false);
            return actionResult;
        } else{
            json =getFormalOrderJson(orderChannel);
        }
        if(json == null){
            logger.info("orderChannel json:"+json);
            actionResult.setMessage("collectOrder异常：businessid类型错误");
            actionResult.setSuccess(false);
            return actionResult;
        }
        try {
            logger.info("orderChannel json:"+json);
            //抢派
            actionResult = JSON.parseObject(IceOkHttpUtils.doHttpPost(CommonConstant.URL_GAWOTWO_COLLECTORDER, json, 30000), ActionResult.class);
            logger.info("collectOrder返回结果：" + IceFastJsonUtils.toJsonString(actionResult));
        } catch (Exception e) {
            logger.info("collectOrder异常：" + e.getMessage());
            actionResult.setMessage("collectOrder异常：" + e.getMessage());
            actionResult.setSuccess(false);
        }
        //抢派
        logService.addLog(actionResult, json, CommonConstant.URL_GAWOTWO_COLLECTORDER, CommonConstant.INTERFACE_TYPE_GAWOTWOCOLLECT);
        return actionResult;
    }
    //意向单json
    private String getCollectOrderJson(OrderChannel params) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{\n");
        stringBuffer.append("\"platformid\": \"12\",\n");
        stringBuffer.append("\"bespeakId\": \"").append(params.getOrderCode()).append("\",\n");
        stringBuffer.append("\"time\": \"").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(params.getOrderDate())).append("\",\n");
        stringBuffer.append("\"channelType\": \"sfmall\",\n");
        stringBuffer.append("\"inModeCode\": \"sf009\",\n");
        stringBuffer.append("\"inModeName\": \"电商社会渠道\",\n");
        stringBuffer.append("\"tradeTypeCode\": \"").append(params.getBusinessId()).append("\",\n");
        stringBuffer.append("\"tradeTypeName\": \"融合冰激凌\",\n");
        stringBuffer.append("\"netTypeCode\": \"40\",\n");
        stringBuffer.append("\"netType\": \"互联网接入\",\n");
        stringBuffer.append("\"eparchyCode\": \"").append(params.getCityCode()).append("\",\n");
        stringBuffer.append("\"eparchyName\": \"").append(params.getCityName()).append("\",\n");
        stringBuffer.append("\"cityCode\": \"").append(params.getDistrictCode()).append("\",\n");
        stringBuffer.append("\"cityName\": \"").append(params.getDistrictName()).append("\",\n");
        stringBuffer.append("\"custName\": \"").append(params.getCustomer()).append("\",\n");
        stringBuffer.append("\"userPhone\": \"").append(params.getPhone()).append("\",\n");
        stringBuffer.append("\"certNo\": \"").append(params.getCardid()).append("\",\n");
        stringBuffer.append("\"installAddr\": \"").append(params.getAddress()).append("\",\n");
        if (IceStringUtils.isBlank(params.getServiceCode()))
        {
            stringBuffer.append("\"serailNumber\": \"\",\n");
        }
        else
        {
            stringBuffer.append("\"serailNumber\": \"").append(params.getServiceCode()).append("\",\n");
        }
        if (IceStringUtils.isBlank(params.getProductName())){
            stringBuffer.append("\"productName\": \"\",\n");
        }
        else {
            stringBuffer.append("\"productName\": \"").append(params.getProductName()).append("\",\n");
        }
        if (IceStringUtils.isBlank(params.getProductSubCode())){
            stringBuffer.append("\"productId\": \"\",\n");
        }
        else {
            stringBuffer.append("\"productId\": \"").append(params.getProductSubCode()).append("\",\n");
        }
        stringBuffer.append("\"price\": \"\",\n");
        stringBuffer.append("\"speed\": \"\",\n");
        stringBuffer.append("\"tariffType\": \"\",\n");
        stringBuffer.append("\"appointment\": \"").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(params.getPreacceptDate())).append("\",\n");
        if (IceStringUtils.isBlank(params.getDepartCode())){
            stringBuffer.append("\"devId\": \"\",\n");
        }
        else {
            stringBuffer.append("\"devId\": \"").append(params.getDepartCode()).append("\",\n");
        }
        stringBuffer.append("\"devName\": \"\",\n");
        stringBuffer.append("\"devDepartCode\": \"\",\n");
        stringBuffer.append("\"puchPersonCode\": \"\",\n");
        stringBuffer.append("\"puchPersonName\": \"\",\n");
        stringBuffer.append("\"puchAreaCode\": \"\",\n");
        stringBuffer.append("\"puchAreaName\": \"\",\n");
        if (IceStringUtils.isBlank(params.getRemark())){
            stringBuffer.append("\"remark\": \"\",\n");
        }
        else {
            stringBuffer.append("\"remark\": \"").append(params.getRemark()).append("\",\n");
        }
        stringBuffer.append("\"rsrvStr1\": \"\",\n");
        stringBuffer.append("\"rsrvStr2\": \"\",\n");
        stringBuffer.append("\"rsrvStr3\": \"\",\n");
        stringBuffer.append("\"memberInfo\": []\n");
        stringBuffer.append("}");
        return stringBuffer.toString();
    }
    //正式单json
    private String getFormalOrderJson(OrderChannel params) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{\n");
        stringBuffer.append("\"platformid\": \"12\",\n");
        if (IceStringUtils.isBlank(params.getBusinessId())){
            stringBuffer.append("\"businessid\": \"\",\n");
        }
        else {
            stringBuffer.append("\"businessid\": \"").append(params.getBusinessId()).append("\",\n");
        }
        stringBuffer.append("\"ordercode\": \"").append(params.getOrderCode()).append("\",\n");
        stringBuffer.append("\"orderdate\": \"").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\",\n");
        if (IceStringUtils.isBlank(params.getServiceCode()))
        {
            stringBuffer.append("\"serailNumber\": \"\",\n");
        }
        else {
            stringBuffer.append("\"servicecode\": \"").append(params.getServiceCode()).append("\",\n");
        }stringBuffer.append("\"nettypecode\": \"50\",\n");
        stringBuffer.append("\"city\": \"").append(params.getCityCode()).append("\",\n");
        stringBuffer.append("\"district\": \"").append(params.getDistrictCode()).append("\",\n");
        stringBuffer.append("\"linkman\":\"").append(params.getCustomer()).append("\",\n");
        stringBuffer.append("\"phone\": \"").append(params.getPhone()).append("\",\n");
        //上门地址
        stringBuffer.append("\"address\": \"").append(params.getAddress()).append("\",\n");
        //装机地址
        stringBuffer.append("\"installaddress\": \"\",\n");
        stringBuffer.append("\"customer\": \"").append(params.getCustomer()).append("\",\n");
        stringBuffer.append("\"cardid\": \"").append(params.getCardid()).append("\",\n");
        stringBuffer.append("\"preacceptdate\":\"").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(params.getPreacceptDate())).append("\",\n");
        if (IceStringUtils.isBlank(params.getProductSubCode())){
            stringBuffer.append("\"productcode\": \"\",\n");
        }
        else {
            stringBuffer.append("\"productcode\":\"").append(params.getProductSubCode()).append("\",\n");
        }
        if (IceStringUtils.isBlank(params.getDepartCode())){
            stringBuffer.append("\"recomPersonId\": \"\",\n");
        }
        else {
            stringBuffer.append("\"recomPersonId\": \"").append(params.getDepartCode()).append("\",\n");
        }
        stringBuffer.append("\"recomDepartId\": \"\",\n");
        stringBuffer.append("\"districtid\": \"\",\n");
        stringBuffer.append("\"autoflag\": \"\",\n");
        stringBuffer.append("\"defaultpersonid\": \"\",\n");
        if (IceStringUtils.isBlank(params.getRemark())){
            stringBuffer.append("\"remark\": \"\",\n");
        }
        else {
            stringBuffer.append("\"remark\": \"").append(params.getRemark()).append("\",\n");
        }
        stringBuffer.append("\"msg\": {");
        stringBuffer.append("\"certtype\":\"\",\n");
        stringBuffer.append("\"certnbr\":\"\",\n");
        stringBuffer.append("\"changeinfo\":\"\",\n");
        stringBuffer.append("\"memberinfo\":[]");
        stringBuffer.append("}");
        stringBuffer.append("}");
        return stringBuffer.toString();
    }

    /**
     * 添加日志
     */
//    private void addLog(ActionResult result, String paramString, String url, int interfaceType) {
//        InterfaceLog log = new InterfaceLog();
//        log.setResultInfo(IceJsonStringUtils.toJsonString(result));
//        if(paramString == null){
//            log.setPostInfo(url);
//        }else{
//            log.setPostInfo(paramString);
//        }
//        log.setUrl(url);
//        log.setCallDate(new Date());
//        log.setInterfaceType(interfaceType);
//        log.setCallState(result.isSuccess() ? CommonConstant.CALL_STATE_SUCCESS : CommonConstant.CALL_STATE_FAIL);
//        logService.saveInterfaceLog(log);
//    }


}
